//
//  YWTopicViewController.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/17.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "YWTopicViewController.h"
#import "YWTopicCell.h"
#import "YWHttpTool.h"
#import "YWTopicDataModels.h"
#import "YWTopicDetailTableViewController.h"
#import <MJRefresh.h>

static NSString *const CellReuseIdentifier = @"YWTopicCell";

@interface YWTopicViewController ()
@property (nonatomic,copy) NSArray *itemArray;

@end

@implementation YWTopicViewController


-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [_tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:CellReuseIdentifier bundle:nil] forCellReuseIdentifier:CellReuseIdentifier];
    self.tableView.rowHeight = 300;
    self.tableView.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
    
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        [self requestData];

        
    }];
         self.tableView.mj_header = header;
    NSArray *picArr = @[[UIImage imageNamed:@"deliveryStaff0"],[UIImage imageNamed:@"deliveryStaff1"],[UIImage imageNamed:@"deliveryStaff2"],[UIImage imageNamed:@"deliveryStaff3"],[UIImage imageNamed:@"staticDeliveryStaff"]];
    
    [header setImages:picArr forState:MJRefreshStatePulling];
    [header setImages:picArr forState:MJRefreshStateRefreshing];
//    header.stateLabel.hidden = YES;


}

-(void)requestData
{
    __weak typeof(self) weakSelf = self;
    [YWHttpTool GET:TOPICURL params:nil success:^(NSDictionary *responseObject) {
        
        YWTopicNSObject *data = [YWTopicNSObject modelObjectWithDictionary:responseObject];
        weakSelf.itemArray = data.myProperty1;
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        
    }];
}




#pragma mark - UITableViewDelegate   and   dataSource -

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _itemArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWTopicInternalBaseClass1 *model = _itemArray[indexPath.row];
    YWTopicCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentifier];
    
    cell.model = model;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    [UIView animateWithDuration:1 animations:^{
    cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWTopicInternalBaseClass1 *model           = _itemArray[indexPath.row];
    YWTopicDetailTableViewController *detailVC = [YWTopicDetailTableViewController new];
    detailVC.url                               = model.topicId;
    [self.navigationController pushViewController:detailVC animated:YES];
}
@end
